#include "MainDialog.h"
#include "ui_sign_scan_select.h"

#include <QMessageBox>
#include <QFileDialog>
#include <QDebug>
#include <QFile>
#include <QMenu>

#include "DialogInput.h"
#include "ProgressDialog.h"

MainDialog::MainDialog(QWidget *parent, IConfig *config) :
    QDialog(parent), ui(new Ui::Sign_Scanner), mConfig(config)
{
    ui->setupUi(this);
    ui->editByteArray->setFocus();

    refreshWidgets();
}

MainDialog::~MainDialog()
{
    delete ui;
}

void MainDialog::on_scan_clicked()
{
    ProgressDialog *winScanState(NULL);
    ui->logLine->setText("");

    if (ui->selectFile->isChecked())
    {
        if (!QFile::exists(ui->editFileName->text()))
        {
            ui->logLine->setStyleSheet("QLabel{color: rgb(255, 50, 50);}");
            ui->logLine->setText("File not found");
            ui->scan->setEnabled(false);
            return;
        } else
            winScanState = new ProgressDialog(this, ui->editFileName->text(), true, mConfig);

    } else if (ui->selectPath->isChecked())
    {
        if (!QFile::exists(ui->editPath->text()))
        {
            ui->logLine->setStyleSheet("QLabel{color: rgb(255, 50, 50);}");
            ui->logLine->setText("Path not found");
            ui->scan->setEnabled(false);
            return;
        } else
            winScanState = new ProgressDialog(this, ui->editPath->text(), false, mConfig);

    } else
    {
        QByteArray array(ui->editByteArray->text().toAscii());
        winScanState = new ProgressDialog(this, array, mConfig);
    }

    if (winScanState)
    {
        winScanState->setModal(true);
        winScanState->show();
    }
}

void MainDialog::on_dialogEnterHEX_clicked()
{
    DialogInput dialogInput(this);
    dialogInput.setData(ui->editByteArray->text());

    dialogInput.exec();
    if (dialogInput.result()  == QDialog::Accepted)

    ui->editByteArray->setText(dialogInput.getData());
}

void MainDialog::on_selectFileDialog_clicked()
{
    QFileDialog fileDialog(this, Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint);
    fileDialog.setFileMode(QFileDialog::ExistingFile);

    if (QFile::exists(ui->editFileName->text()))
        fileDialog.setDirectory(ui->editFileName->text());

    if (!fileDialog.exec()) return;

    QString str = fileDialog.selectedFiles().at(0);
    ui->editFileName->setText(str);
    ui->selectFile->setChecked(true);
}

void MainDialog::on_selectPathDialog_clicked()
{
    QFileDialog fileDialog(this, Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint);
    fileDialog.setFileMode(QFileDialog::Directory);

    if (QFile::exists(ui->editPath->text()))
        fileDialog.setDirectory(ui->editPath->text());

    if (!fileDialog.exec()) return;

    QString str = fileDialog.selectedFiles().at(0);
    ui->editPath->setText(str);
    ui->selectPath->setChecked(true);
}

void MainDialog::on_selectByteArray_clicked()
{
    refreshWidgets();
    ui->scan->setEnabled(!ui->editByteArray->text().isEmpty());
    ui->editByteArray->setFocus();
}

void MainDialog::on_selectFile_clicked()
{
    refreshWidgets();
    ui->scan->setEnabled(!ui->editFileName->text().isEmpty());
    ui->editFileName->setFocus();
}

void MainDialog::on_selectPath_clicked()
{
    refreshWidgets();
    ui->scan->setEnabled(!ui->editPath->text().isEmpty());
    ui->editPath->setFocus();
}

void MainDialog::on_editByteArray_textChanged(const QString &arg1)
{
    if (ui->selectByteArray->isChecked())
        ui->scan->setEnabled(arg1.length());
}

void MainDialog::on_editFileName_textChanged(const QString &arg1)
{
    if (ui->selectFile->isChecked())
        ui->scan->setEnabled(arg1.length());
}


void MainDialog::on_editPath_textChanged(const QString &arg1)
{
    if (ui->selectPath->isChecked())
        ui->scan->setEnabled(arg1.length());
}

void MainDialog::refreshWidgets()
{
    ui->editByteArray->setEnabled(ui->selectByteArray->isChecked());
    ui->dialogEnterHEX->setEnabled(ui->selectByteArray->isChecked());

    ui->editFileName->setEnabled(ui->selectFile->isChecked());
    ui->selectFileDialog->setEnabled(ui->selectFile->isChecked());

    ui->editPath->setEnabled(ui->selectPath->isChecked());
    ui->selectPathDialog->setEnabled(ui->selectPath->isChecked());
}
